﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Configuration;
using MiniExcelLibs;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using SH.Common.CommonReturn;
using SH.Common.Helper;
using SH.Customer.Application.IApplication;
using SH.Customer.Application.IApplication.ICustomer;
using SH.Customer.Domain;
using SH.Customer.Domain.CustomerClass;
using System.Net.Http.Headers;

namespace SH.Customer.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustomerController : ControllerBase
    {
        private readonly ICustomerApplication _customerApplication;

        public CustomerController(ICustomerApplication customerApplication)
        {
            _customerApplication = customerApplication;
        }

        [HttpGet]
        public IActionResult Index()
        {
            return Ok(DateTime.Now);
        }


        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadImage(IFormFile file)
        {
            // 调用静态方法  
            HttpResult greeting = QiniuHelper.UploadFile(file);

            return Ok(greeting);
        }

        /// <summary>
        /// 文件导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportCustomer()
        {
            var customers = await _customerApplication.GetListAsync();

            List<object> list = new List<object>();

            foreach (var item in customers)
            {
                list.Add(new
                {
                    客户名称 = item.CName,
                    手机号码 = item.Tel,
                    出生日期 = item.Birthday,
                    余额 = item.Balance,
                    积分 = item.Integral,
                    省份证号 = item.IdCardNumber,
                });
            }

            var memoryStream = new MemoryStream();

            memoryStream.SaveAs(list);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "客户信息.xlsx"
            };
        }


        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="customerDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CustomerAdd(CustomerDto customerDto)
        {
            var result = await _customerApplication.CustomerAdd(customerDto);
            return Ok(result);
        }

        /// <summary>
        /// 获取客户信息列表
        /// </summary>
        /// <param name="ctId"></param>
        /// <param name="tel"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult CustomerInfoList(int ctId, string? tel, int pageIndex, int pageSize)
        {
            var list = _customerApplication.CustomerInfoList(ctId, tel, pageIndex, pageSize);
            return Ok(list);
        }

        /// <summary>
        /// 获取客户类型字典列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCustomerTypeDicsList()
        {
            var list = _customerApplication.GetCustomerTypeDicsList();
            return Ok(list);
        }

        /// <summary>
        /// 获取国家字典列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetNationDicsList()
        {
            var list = _customerApplication.GetNationDicsList();
            return Ok(list);
        }

        /// <summary>
        /// 客户信息列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllCustomersAsync()
        {
            var result = await _customerApplication.GetListAsync();


            return Ok(result);
        }


        /// <summary>
        /// 获取客户信息详情
        /// </summary>
        /// <param name="cId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCustomerDelitalAsync(int cId)
        {
            var list = await _customerApplication.GetCustomerDelitalAsync(cId);
            return Ok(list);
        }

        /// <summary>
        /// 客户信息修改
        /// </summary>
        /// <param name="customerUpdDto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdCustomer(CustomerUpdDto customerUpdDto)
        {
            var list = _customerApplication.UpdCustomer(customerUpdDto);
            return Ok(list);
        }
    }
}
